using System;

using System.Configuration;

namespace TimelineNet.Library {
    [Serializable()]
    public class Event {

        public DateTime Start;
        public DateTime End;
        public bool IsDuration = false;
        public string Title;
        public string Image;
        public string Thumbnail;
        public string Description;
        public string Color;
        public string TextColor;

        public DateTime LatestStart;
        public DateTime EarliestEnd;

        public string Link;
        public string Icon;

        public bool RenderAsXML = true;
        public Event() {
        }

        public Event(DateTime Start, DateTime End, bool IsDuration, string Title, string Image, string Description, string Thumbnail) {
            this.Start = Start;
            this.End = End;
            this.IsDuration = IsDuration;
            this.Title = Title;
            this.Image = Image;
            this.Description = Description;
            this.Thumbnail = Thumbnail;
        }


        public override string ToString() {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if(RenderAsXML) {
                sb.Append("<event ");

                bool hasStart = false;
                bool hasEnd = false;
                if(Start != null  && Start!=DateTime.MinValue) {
                    hasStart = true;
                    sb.Append(string.Format(" start=\"{0}\"", Timeline.FormatDate(Start)));
                }

                if(End != null && End != DateTime.MinValue)
                {
                    hasEnd = true;
                    sb.Append(string.Format(" end=\"{0}\"", Timeline.FormatDate(End)));
                }

                if(LatestStart != null && LatestStart != DateTime.MinValue) sb.Append(string.Format(" latestStart=\"{0}\"", Timeline.FormatDate(LatestStart)));
                if(EarliestEnd != null && EarliestEnd != DateTime.MinValue) sb.Append(string.Format(" earliestEnd=\"{0}\"", Timeline.FormatDate(EarliestEnd)));

                
                if(IsDuration || (hasStart && hasEnd)) sb.Append(string.Format(" isDuration=\"{0}\"", IsDuration.ToString().ToLower()));
                if(Title != null && Title != "") sb.Append(string.Format(" title=\"{0}\"", System.Web.HttpUtility.HtmlEncode(Title)));

                if (Image != null && Image != "") sb.Append(string.Format(" image=\"{0}\"", Image));
                if(Thumbnail != null && Thumbnail != "") sb.Append(string.Format(" thumbnail=\"{0}\"", Thumbnail));
                if(Color != null && Color != "") sb.Append(string.Format(" color=\"{0}\"", Color));
                if(TextColor != null && TextColor != "") sb.Append(string.Format(" textColor=\"{0}\"", TextColor));

                if (Link != null && Link != "") sb.Append(string.Format(" link=\"{0}\"", Link));
                if(Icon != null && Icon != "") sb.Append(string.Format(" icon=\"{0}\"", Icon));
                sb.Append(">");
                string desc = System.Web.HttpUtility.HtmlEncode(this.Description);
                //string desc = this.Description.Replace("&", "&amp;").Replace(">", "&gt;").Replace("<", "&lt;").Replace("\"", "&#34;");
                sb.Append(desc);
                sb.Append("</event>");
            } else {

            }
            return sb.ToString();
        }
    }
}